package cn.liwq.demo.web.servlet;

import cn.liwq.demo.web.annotation.FooController;
import cn.liwq.demo.web.annotation.FooRequestMapping;
import cn.liwq.demo.web.handler.ControllerHandler;
import cn.liwq.demo.web.util.ClassScanner;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>Project: demo-common </p>
 * 
 * <p>Function:  </p>
 * 
 * <p>Description:  </p>
 * 
 * <p>Copyright: Copyright(c) 2018 Ucarinc </p>
 * 
 * <p>Company: Ucarinc </p>
 * 
 * @author weiqiang.li@ucarinc.com
 * 
 * @date 2018-08-24 10:44:11
 * 
 * @version 1.0
 */
public class FooDispatchServlet extends HttpServlet {

    private static final Logger log = Logger.getLogger(FooDispatchServlet.class);

    private static Map<String, Class<?>> controllerMap = new HashMap<>();

    private static ControllerHandler handler = new ControllerHandler();

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        BasicConfigurator.configure();
        String packageStr = config.getInitParameter("scan-package");
        List<Class<?>> controllerList = ClassScanner.loadClass(packageStr, FooController.class);
        initControllerMap(controllerList);
    }

    private void initControllerMap(List<Class<?>> classList) {
        for (Class<?> aClass : classList) {
            FooRequestMapping fooRequestMapping = aClass.getAnnotation(FooRequestMapping.class);
            controllerMap.put(fooRequestMapping.value(), aClass);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        handler.handle(req, resp, controllerMap.get(getControllerPath(req)));
    }

    private String getControllerPath(HttpServletRequest httpServletRequest) {
        String contextPath = httpServletRequest.getContextPath();
        String uri = httpServletRequest.getRequestURI();
        String path = null;
        int pos = uri.indexOf(contextPath);
        path = uri.substring(pos + contextPath.length());
        log.info("path=" + path);
        Set<String> keySet = controllerMap.keySet();
        int matchLen = -1;
        String controllerPath = null;
        for (String key : keySet) {
            if (path.startsWith(key) && key.length() > matchLen) {
                controllerPath = key;
            }
        }
        return controllerPath;
    }

    @Override
    public void destroy() {
        super.destroy();
        controllerMap.clear();
    }
}
